import {objectExistsInArray} from '@/views/youshuda/utils/youshuda'
import {
  InputComponent,
  SelectComponents,
  LayoutComponents,
  ChartComponents,
  DataComponents,
  CustomComponents,
  TableComponents } from "@/views/youshuda/components/FormBuild/utils/registerType"

const state = {
  //所有组件渲染集合
  componentRenders: {},
  //组件属性配置信息
  componentConfig: {},
  //所有组件属性配置集合
  componentConfigVue: {},
  //所有组件路径集合
  componentPath: {},
  //组件对应关系
  ComponentMatchRelation:{},
  //输入型组件
  inputComponents: [],
  inputComponentsByMobile: [],
  //选择型组件
  selectComponents: [],
  selectComponentsByMobile: [],
  //布局型组件
  layoutComponents: [],
  layoutComponentsByMobile: [],
  //图表型组件
  chartComponents: [],
  chartComponentsByMobile: [],
  //数据型组件
  dataComponents: [],
  dataComponentsByMobile: [],
  //自定义组件
  customComponents: [],
  customComponentsByMobile: [],
  //数据表格组件
  tableComponents:[],
  tableComponentsByMobile:[]
}
const mutations = {
  // 保存组件
  PutComponent: (state,{elType,clientType,config,configVue,componentPath,renderComponent}) =>{
    //以配置文件中moduleType为组件唯一标识
    let moduleType = config['moduleType']
    //添加组件渲染集合
    state.componentRenders[moduleType]=renderComponent
    //组件属性配置信息
    state.componentConfig[moduleType]=config
    //组件属性配置页面集合
    state.componentConfigVue[moduleType]=configVue
    //组件路径集合
    state.componentPath[moduleType]=componentPath

    if(elType == InputComponent && !objectExistsInArray(state.inputComponents,config)){
      if(clientType == 'PC'){
        state.inputComponents.push(config)
      }else{
        state.inputComponentsByMobile.push(config)
      }
    }
    if(elType == SelectComponents && !objectExistsInArray(state.selectComponents,config)){
      // 保存选择型组件
      if(clientType == 'PC') {
        state.selectComponents.push(config)
      }else{
        state.selectComponentsByMobile.push(config)
      }
    }
    if(elType == LayoutComponents && !objectExistsInArray(state.layoutComponents,config)){
      // 保存布局型组件
      if(clientType == 'PC') {
        state.layoutComponents.push(config)
      }else {
        state.layoutComponentsByMobile.push(config)
      }
    }
    if(elType == ChartComponents && !objectExistsInArray(state.chartComponents,config)){
      // 保存图表型组件
      if(clientType == 'PC') {
        state.chartComponents.push(config)
      }else {
        state.chartComponentsByMobile.push(config)
      }
    }
    if(elType == DataComponents && !objectExistsInArray(state.dataComponents,config)){
      // 保存数据型组件
      if(clientType == 'PC') {
        state.dataComponents.push(config)
      }else {
        state.dataComponentsByMobile.push(config)
      }
    }
    if(elType == CustomComponents && !objectExistsInArray(state.customComponents,config)){
      // 保存自定义型组件
      if(clientType == 'PC') {
        state.customComponents.push(config)
      }else {
        state.customComponentsByMobile.push(config)
      }
    }
    if(elType == TableComponents && !objectExistsInArray(state.tableComponents,config)){
      // 数据表格组件
      if(clientType == 'PC') {
        state.tableComponents.push(config)
      }else {
        state.tableComponentsByMobile.push(config)
      }
    }
  },
  //记录PC端组件路径与移动端组件路径对应关系
  RecordComponentMatchRelation: (state,{pcComponent,mobileComponent}) =>{
    state.ComponentMatchRelation[pcComponent]=mobileComponent
    state.ComponentMatchRelation[mobileComponent]=pcComponent
  }
}

const actions = {
  // 保存组件
  PutComponent({ commit }, data){
    commit('PutComponent', data)
  },
  // 记录PC端组件路径与移动端组件路径对应关系
  RecordComponentMatchRelation({ commit }, data){
    commit('RecordComponentMatchRelation', data)
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

